package by.amakarevich.webapp.page.kit.details;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.resource.UrlResourceReference;

import by.amakarevich.datamodel.Kit;
import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.ShoppingCartInfo;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.ShoppingCartInfoService;
import by.amakarevich.services.ShoppingCartService;
import by.amakarevich.webapp.app.BasicAuthenticationSession;
import by.amakarevich.webapp.page.BaseLayout;
import by.amakarevich.webapp.page.login.LoginPage;

public class KitDetailsPage extends BaseLayout{
	@Inject
	private ShoppingCartInfoService shoppingCartInfoService;
	@Inject
	private ShoppingCartService shoppingCartService;
	
	private Kit kit;
	private Product product;
	
	public KitDetailsPage(Kit kit, Product product){
		this.kit=kit;
		this.product=product;
	}
	
	@Override
	protected void onInitialize() {
		super.onInitialize();

		WebMarkupContainer liContainer = new WebMarkupContainer("wr-li");
		add(liContainer);

		WebMarkupContainer hContaienr = new WebMarkupContainer("wr-h");
		liContainer.add(hContaienr);

	
		hContaienr.add(new Label("name", new Model<String>(kit.getName())));
		String urlStr="http://localhost:8081/imagesService/"+product.getId().toString()+".jpg";
		final UrlResourceReference url= new UrlResourceReference(Url.parse(urlStr));
		liContainer.add(new Image("img", url));
		liContainer.add(new Label("canvas", new Model<Long>(kit.getCanvas())));
		liContainer.add(new Label("height", new Model<Long>(kit.getHeight())));
		liContainer.add(new Label("width", new Model<Long>(kit.getWidth())));
		liContainer.add(new Label("company", new Model<String>(product.getCompany())));
		liContainer.add(new Label("available", new Model<Long>(product.getAvailable())));
		liContainer.add(new Label("kitColor", new Model<String>(kit.getKitColor())));
		liContainer.add(new Label("price", new Model<Long>(product.getPrice())));
		liContainer.add(new Label("specification", new Model<String>(kit.getSpecification())));
		
		
		add(new Link("product-to-shopping-cart-link") {
			@Override
			public void onClick() {
				if(BasicAuthenticationSession.get().getUser()!=null){
					UserAccount user= BasicAuthenticationSession.get().getUser();
				
					ShoppingCart shoppingCart = shoppingCartService.getByUser(user) ;
				
				
					ShoppingCartInfo shoppingCartInfo = new ShoppingCartInfo();
					if(shoppingCartInfoService.getShoppingCartInfo(shoppingCart, product)==null){
						shoppingCartInfo.setQuantity(1l);
						shoppingCartInfo.setPrice(product.getPrice());
						shoppingCartInfoService.addNewShoppingCartInfo(shoppingCartInfo, product, shoppingCart);
					}else{
						shoppingCartInfo=shoppingCartInfoService.getShoppingCartInfo(shoppingCart, product);
						shoppingCartInfo.setQuantity(shoppingCartInfo.getQuantity()+1);
						shoppingCartInfoService.updateShoppingCartInfo(shoppingCartInfo);						
					}
					Page page = getPage();

					page.info("Артикул добавлен в корзину");

					setResponsePage(page);
					
				}else{
					setResponsePage(new LoginPage());
				}
			}
		});
	}
	@Override
	protected IModel<String> getPageTitle() {
		return new Model<String>("p.kit.details.title");
	}

}